//; ---------------------------------------------------------------------------
//; Object 2B - Chopper enemy (GHZ)
//; ---------------------------------------------------------------------------


#include "chopper.h"

#include "../../sprite.h"
#include "../../objectsystemNEWER.h"

static object *obj;

//EXTERN DEFINITIONS HERE


//; ---------------------------------------------------------------------------
//; Animation script - Chopper enemy
//; ---------------------------------------------------------------------------
byte slow[4] = { 7, 0, 1, afEnd };
byte fast[4] = { 3, 0, 1, afEnd };
byte still[4] = { 7, 0, afEnd };

byte *Ani_Chop[3] = { slow, fast, still };

//; ---------------------------------------------------------------------------
//; Sprite mappings - Chopper enemy (GHZ)
//; ---------------------------------------------------------------------------
byte mouthshut[6] = { 1,
                   0xF0, 0xF, 0,    0, 0xF0
};

byte mouthopen[6] = { 1,
		   0xF0, 0xF, 0, 0x10, 0xF0
};

byte *Map_Chop[2] = { mouthshut, mouthopen };
//==========

void (*Chop_Index[2])() = { Chop_Main, Chop_ChgSpeed };

//OBJECT:					; XREF: Obj_Index
void Chopper(object *_obj)
{
    obj = _obj;

    //moveq	#0,d0
    //move.b	obRoutine(a0),d0
    byte routine = obj->Routine/2;

    //move.w	Chop_Index(pc,d0.w),d1
    //jsr	Chop_Index(pc,d1.w)
    (*Chop_Index[routine])();

    //bra.w	RememberState
    RememberState(obj);
}

//; ===========================================================================

#define origY 0x30

//OBJECT_Main:	; Routine 0
void Chop_Main()
{
    //addq.b	#2,obRoutine(a0)
    obj->Routine = obj->Routine + 2;

    //move.l	#Map_Chop,obMap(a0)
    obj->Map = Map_Chop;

    //move.w	#$47B,obGfx(a0)
    obj->Gfx = 0x47B;

    //move.b	#4,obRender(a0)
    obj->Render = 4;

    //move.b	#4,obPriority(a0)
    obj->Priority = 4;

    //move.b	#9,obColType(a0)
    obj->ColType = 9;

    //move.b	#$10,obActWid(a0)
    obj->ActionWidth = 0x10;

    //move.w	#-$700,obVelY(a0) ; set vertical speed
    obj->VelocityY = -0x700;
 
    //move.w	obY(a0),origY(a0) ; save original position
    ptrsetobjvariableword(origY, obj, obj->Y);

    Chop_ChgSpeed();

}

//Chop_ChgSpeed:	; Routine 2
void Chop_ChgSpeed()
{
    //lea	(Ani_Chop).l,a1
    byte **aniScript = Ani_Chop;

    //bsr.w	AnimateSprite
    AnimateSprite(obj, aniScript);

    //bsr.w	SpeedToPos
    SpeedToPos(obj);

    //addi.w	#$18,obVelY(a0)	; reduce speed
    obj->VelocityY = obj->VelocityY + 0x18;

    //move.w	origY(a0),d0
    word originalY = ptrgetobjvariableword(origY, obj);

    //cmp.w	obY(a0),d0	; has Chopper returned to its original position?
    //bcc.s	@chganimation	; if not, branch
    if (originalY < obj->Y)
    {
        //move.w	d0,obY(a0)
        obj->Y = originalY;

        //move.w	#-$700,obVelY(a0) ; set vertical speed
        obj->VelocityY = -0x700;

    }   

    //@chganimation:
    //move.b	#1,obAnim(a0)	; use fast animation
    obj->Anim = 1;

    //subi.w	#$C0,d0
    word highPoint = originalY - 0xC0;

    //cmp.w	obY(a0),d0 
    //bcc.s	@nochg
    if (highPoint < obj->Y)
    {
        //move.b	#0,obAnim(a0)	; use slow animation
        obj->Anim = 0;

        //tst.w	obVelY(a0)	; is Chopper at	its highest point?
        //bmi.s	@nochg		; if not, branch
        if (obj->VelocityY > 0)
        {
	    //move.b	#2,obAnim(a0)	; use stationary animation
            obj->Anim = 2;
        }
	
    } //@nochg:
}     //rts	
